package news.service;

import news.example.Topic;
import news.util.BaseDao;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zht
 * 查询
 */
public class QueryTopicServiceImpl implements IQueryTopicService{
    private BaseDao baseDao = new BaseDao();

    @Override
    public List<Topic> queryAllTopic() {
        List<Topic> list = null;
        try {
            baseDao.openDB();
            String sql = "select * from news_topic";
            baseDao.st = baseDao.conn.createStatement();
            baseDao.rs = baseDao.st.executeQuery(sql);
            list = new ArrayList<Topic>();

            while (baseDao.rs.next()){
                Topic topic = new Topic();
                topic.setId(baseDao.rs.getInt(1));
                topic.setTopicName(baseDao.rs.getString(2));
                topic.setCreateTime(baseDao.rs.getDate(3));
                list.add(topic);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<Topic> findNameById(Integer id) {
        List<Topic> list = null;
        try {
            baseDao.openDB();
            String sql = "select topicname from news_topic where id = ?";
            baseDao.ps = baseDao.conn.prepareStatement(sql);
            baseDao.ps.setInt(1,id);
            baseDao.rs = baseDao.ps.executeQuery();
            list = new ArrayList<Topic>();
            while (baseDao.rs.next()){
                Topic topic = new Topic();
                topic.setTopicName(baseDao.rs.getString("topicname"));
                list.add(topic);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }
}
